package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cskaoyan.wordmemorize.dao.entity.UserVocRevDO;
import com.cskaoyan.wordmemorize.dao.mapper.UserVocRevMapper;
import com.cskaoyan.wordmemorize.service.UserVocService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;

/**
 * 用户单词服务实现类
 */
@Service
@RequiredArgsConstructor
public class UserVocServiceImpl implements UserVocService {

    private final UserVocRevMapper userVocRevMapper;

    // TODO 未使用
    @Override
    @Transactional
    public void selectVocForReview(Long vocId, Long userId) {
        // 检查是否已经选择过
        LambdaQueryWrapper<UserVocRevDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserVocRevDO::getVocId, vocId)
                .eq(UserVocRevDO::getUserId, userId);
        UserVocRevDO existingRev = userVocRevMapper.selectOne(queryWrapper);

        if (existingRev == null) {
            // 创建新的记录
            UserVocRevDO userVocRevDO = new UserVocRevDO();
            userVocRevDO.setVocId(vocId);
            userVocRevDO.setUserId(userId);
            userVocRevDO.setFactor(2.5); // 默认因子
            userVocRevDO.setReviewInterval(1); // 默认间隔
            userVocRevDO.setNextReviewDate(LocalDate.now()); // 今天复习
            userVocRevDO.setType(0); // 默认类型
            userVocRevDO.setFamiliarity(0); // 默认熟悉度
            userVocRevDO.setConsecutiveFailCount(0); // 默认连续失败次数
            userVocRevDO.setTotalReview(0); // 默认总复习次数

            userVocRevMapper.insert(userVocRevDO);
        }
    }

    // TODO 未使用
    @Override
    @Transactional
    public void unselectVocForReview(Long vocId, Long userId) {
        // 删除选择记录
        LambdaQueryWrapper<UserVocRevDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserVocRevDO::getVocId, vocId)
                .eq(UserVocRevDO::getUserId, userId);

        userVocRevMapper.delete(queryWrapper);
    }

}
